#region Copyright (C) 2005-2006 Team MediaPortal

/* 
 *	Copyright (C) 2005-2006 Team MediaPortal
 *	http://www.team-mediaportal.com
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

using System;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using MediaPortal.Player;
using MediaPortal.GUI.Library;
using MediaPortal.Util;

namespace ProcessPlugins.WMPOSD
{
    public class WMPOSDPlugin : IPlugin, ISetupForm
    {
        #region Fields
        private static OSDProgress _osdProgress;
        #endregion

        #region IPlugin implementation
        private bool started = false;

        public void Start()
        {
            if (!started)
            {
                try
                {
                    g_Player.PlayBackStarted += new g_Player.StartedHandler(g_Player_PlayBackStarted);
                    g_Player.PlayBackEnded += new g_Player.EndedHandler(g_Player_PlayBackEnded);
                    g_Player.PlayBackStopped += new g_Player.StoppedHandler(g_Player_PlayBackStopped);
                    GUIWindowManager.OnNewAction += new OnActionHandler(GUIWindowManager_OnNewAction);
                    started = true;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                
            }
        }

        public void Stop()
        {


        }

        #endregion

        #region Implementation

        private static void GUIWindowManager_OnNewAction(Action action)
        {
            try
            {
                if (action.m_key != null)
                {
                    switch (action.wID)
                    {
                        //Progress
                        case Action.ActionType.ACTION_BIG_STEP_FORWARD:
                        case Action.ActionType.ACTION_BIG_STEP_BACK:
                        case Action.ActionType.ACTION_STEP_BACK:
                        case Action.ActionType.ACTION_STEP_FORWARD:
                            if (_osdProgress != null)
                                if ((g_Player.Playing | g_Player.Paused) & g_Player.FullScreen & g_Player.HasVideo & (g_Player.Player.GetType() == typeof(MediaPortal.Player.AudioPlayerWMP9)))
                                {

                                    if (g_Player.FullScreen)
                                    {
                                        double cs = 0; bool s, e;
                                        cs = g_Player.GetSeekStep(out s, out e) + g_Player.CurrentPosition;
                                        if (cs > g_Player.Duration) cs = g_Player.Duration;
                                        if (cs < 0) cs = 0;
                                        _osdProgress.Show(g_Player.Duration, g_Player.CurrentPosition, cs, g_Player.GetStepDescription(), 5000);
                                    }
                                }
                            break;
                        //Volume
                        case Action.ActionType.ACTION_VOLUME_UP:
                        case Action.ActionType.ACTION_VOLUME_DOWN:
                        case Action.ActionType.ACTION_VOLUME_MUTE:
                            //TODO: Show Volume OSD.
                            break;
                        //Info???
                        case Action.ActionType.ACTION_SHOW_OSD:

                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        private static void g_Player_PlayBackStarted(g_Player.MediaType type, string filename)
        {
            if (g_Player.Player.GetType() == typeof(MediaPortal.Player.AudioPlayerWMP9))
            {
                try
                {
                    if (g_Player.Player.GetType() == typeof(MediaPortal.Player.AudioPlayerWMP9))
                    {
                        if (_osdProgress == null)
                        {
                            _osdProgress = new OSDProgress(Application.OpenForms[0]); 
                            Log.Error("WMPOSD {0}", "Started.");
                        }
                        else
                        {
                            g_Player_PlayBackStopped(g_Player.MediaType.Video, -1, string.Empty);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
               
            }
        }

        private static void g_Player_PlayBackStopped(g_Player.MediaType type, int stoptime, string filename)
        {            
            //Dispose OSD Window(s).
            if (_osdProgress != null)
            {
                _osdProgress.Dispose();
                _osdProgress = null;
                Log.Error("WMPOSD {0}", "Stopped.");
            }        
        }

        private static void g_Player_PlayBackEnded(g_Player.MediaType type, string filename)
        {
            g_Player_PlayBackStopped(type, -1, filename);
        }

        #endregion

        #region ISetupForm implementation

        /// <summary>
        /// Gets the name of this plugin
        /// </summary>
        /// <returns>Name</returns>
        public string PluginName()
        {
            return "Windows Media Player OSD";
        }

        /// <summary>
        /// Gets the description of this plugin
        /// </summary>
        /// <returns>A fixed description</returns>
        public string Description()
        {
            return "OSD for Windows Media Player.";
        }

        /// <summary>
        /// Gets the author of this plugin
        /// </summary>
        /// <returns>Author</returns>
        public string Author()
        {
            return "Nickolay Shvydkyy";
        }

        /// <summary>
        /// Do nothing.
        /// </summary>
        public void ShowPlugin()
        {

        }

        /// <summary>
        /// Returns whether our plugin can be enabled and disabled
        /// </summary>
        /// <returns>true</returns>
        public bool CanEnable()
        {
            return true;
        }

        /// <summary>
        /// Returns the Window ID of this plugin
        /// </summary>
        /// <returns>-1</returns>
        public int GetWindowId()
        {
            return -1;// WindowID;
        }

        /// <summary>
        /// Returns whether this plugin is enabled per default
        /// </summary>
        /// <returns>false</returns>
        public bool DefaultEnabled()
        {
            return true;
        }

        /// <summary>
        /// Returns whether this plugin has a setup screen
        /// </summary>
        /// <returns>true</returns>
        public bool HasSetup()
        {
            return false;
        }

        /// <summary>
        /// If the plugin should have its own button on the home menu of Mediaportal then it
        /// should return true to this method, otherwise if it should not be on home
        /// it should return false
        /// </summary>
        /// <param name="strButtonText">text the button should have</param>
        /// <param name="strButtonImage">image for the button, or empty for default</param>
        /// <param name="strButtonImageFocus">image for the button, or empty for default</param>
        /// <param name="strPictureImage">subpicture for the button or empty for none</param>
        /// <returns>false : plugin does not need its own button on home</returns>
        public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
        {
            strButtonText = null;
            strButtonImage = null;
            strButtonImageFocus = null;
            strPictureImage = null;
            return false;
        }

        #endregion
    }
}
